<?php
$userId = $_GET['userId'];
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="../../lib/layui-v2.8.12/src/layui.js"></script>
  <link rel="stylesheet" href="../../lib/layui-v2.8.12/src/css/layui.css">
  <script src="../../utils/form-serialize.js"></script>
  <script src="../../lib/axios/axios.min.js"></script>
  <link rel="stylesheet" href="../../css/common.css">
  <!-- <link rel="stylesheet" href="../../css/user/userInfo.css"> -->

  <title>Document</title>
</head>

<body>
  <!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
  <form class="layui-form layui-form-pane" id="add" action="./update.php?userId=<?php echo $userId; ?>" method="POST" lay-filter="update">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block" style="width: 200px;">
          <input type="text" name="userName" maxlength="10" minlength="6" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">用户密码</label>
        <div class="layui-input-inline" style="width: 200px;">
          <input type="text" name="userPassword" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">用户昵称</label>
        <div class="layui-input-inline">
          <input type="text" name="nikeName" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">支持球队</label>
      <div class="layui-input-block" style="width: 200px;">
        <select id="selectTeam" name="teamId">
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">性别</label>
      <input type="radio" name="gender" value="1" lay-filter="demo-radio-filter" checked>
      <div lay-radio>
        <span style="color: blue;">男 ♂</span>
      </div>
      <input type="radio" name="gender" value="2" lay-filter="demo-radio-filter">
      <div lay-radio>
        <span style="color: pink;">女 ♀</span>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">生日</label>
      <div class="layui-input-block" style="width: 200px;">
        <input type="text" name="birthday" class="layui-input" id="date" placeholder="yyyy-MM-dd">
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-inline" style="width: 100px;">
          <input name="age" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">手机</label>
        <div class="layui-input-inline" style="width: 200px;">
          <input name="phone" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">地址</label>
        <div class="layui-input-inline" style="width: 150px;">
          <select id="selectAddress" name="address" lay-filter="address">
          </select>
        </div>
      </div>
    </div>


    <div class="layui-form-item btn">
      <div>
        <button class="layui-btn" id="confirm">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <?php
  DEFINE('DB_USER', 'worldcup');
  DEFINE('DB_PASSWORD', '123456');
  DEFINE('DB_HOST', 'localhost');
  DEFINE('DB_NAME', 'worldcup');

  $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$dbc) {
    die('Could not to connect to Mysql:' . mysqli_connect_error());
  }
  mysqli_set_charset($dbc, 'utf8');

  $userId = $_GET['userId'];
  $userSql = "select * from user where user_id = {$userId};";
  $userAaary = mysqli_query($dbc, $userSql);
  if (!$userAaary) {
    echo  mysqli_error($dbc);
  }
  $rows = mysqli_fetch_assoc($userAaary);

  $teamNameSql = "select team_name from team;";
  $teamAaary = mysqli_query($dbc, $teamNameSql);
  // 初始化一个空数组来存储所有行数据
  $dataArray = array();
  // 逐行提取数据并存入数组
  while ($row = mysqli_fetch_assoc($teamAaary)) {
    $dataArray[] = $row['team_name'];
  }
  ?>
  <script>
    //支持球队下拉框
    let ans = <?php echo json_encode($dataArray); ?>;
    var select = document.getElementById('selectTeam');
    for (var i = 1; i <= ans.length; i++) {
      var option = document.createElement("option");
      option.setAttribute("value", i);
      option.innerText = ans[i - 1];
      if (i == ans.length) {
        option.setAttribute("selected", "selected");
      }
      select.appendChild(option)
    }
    //地区的数据
    addressData = ["北京", "天津", "上海", "重庆", "河北", "河南", "云南", "辽宁", "黑龙江", "湖南", "安徽", "山东", "新疆", "江苏", "浙江", "江西", "湖北", "广西", "甘肃", "山西", "内蒙古", "陕西", "吉林", "福建", "贵州", "广东", "青海", "西藏", "四川", "宁夏", "海南", "台湾", "香港", "澳门"]
    var selectAddress = document.getElementById('selectAddress');
    for (var i = 1; i <= addressData.length; i++) {
      var options = document.createElement("option");
      options.setAttribute("value", addressData[i - 1]);
      options.innerText = addressData[i - 1];
      if (i == addressData.length) {
        options.setAttribute("selected", "selected");
      }
      selectAddress.appendChild(options)
    }

    //渲染下拉框
    layui.use(['laydate', 'form'], function() {
      var form = layui.form;
      //渲染支持球队下拉框
      form.render("select");
      //渲染地区下拉框
      form.render("selectAddress");
      //日期选择器
      var laydate = layui.laydate;
      // 渲染
      laydate.render({
        elem: '#date'
      });
    });
    let res = <?php echo json_encode($rows); ?>;
    // console.log(res);
    layui.use(function() {
      var $ = layui.$;
      var form = layui.form;
      // 表单赋值
      form.val('update', {
        "userName": res.user_name,
        "userPassword": res.user_password,
        "nikeName": res.nike_name,
        "teamId": +res.vote_team,
        "gender": +res.gender,
        "age": +res.age,
        "birthday": res.birthday,
        "phone": res.phone,
        "address": res.address
      });
    })
  </script>

</body>

</html>